﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LoghogInterface.Modules;
using System.Data.SqlClient;
using System.Collections;
using System.Windows;
using LoghogInterface.Models;
using System.Windows.Input;
using LoghogInterface.Commands;
using LoghogInterface.Views;

namespace LoghogInterface.ViewModels
{
    /// <summary>
    /// Модель представления главного окна
    /// </summary>
    class ViewModelMainWindow : ViewModelBase
    {
        public ViewModelWindowInput input = new ViewModelWindowInput();
        private ModelConfigDatabase _modelDatabase;
        private int _idJournal;

        public ViewModelMainWindow()
        {
            ParserConfig _parserConfig = new ParserConfig();
            _modelDatabase = _parserConfig.GetDatabaseSettings();
            input.OnInput += new ViewModelWindowInput.InputEventHandler(LoadingData);
        }

        private void LoadingData(int idUser, int idJournal)
        {
            this.LoadGrid(idJournal);
            _idJournal = idJournal;
        }

        private DataView _gridData;

        public DataView GridData
        {
            get
            {
                return _gridData;
            }
            set
            {
                if (_gridData != value)
                {
                    _gridData = value;
                    OnPropertyChanged("GridData");
                }
            }
        }

        private void LoadGrid(int idJournal)
        {
            DataSet dataSet = new DataSet();
            DataTable dataTable = new DataTable();
            JournalSQLHelper _sqlHelper = new JournalSQLHelper();

            var fields = _sqlHelper.GetFieldsJournal(idJournal);

            foreach (var field in fields)
            {
                DataColumn propertyName = new DataColumn(field.NameRus.ToString());
                dataTable.Columns.Add(propertyName);
            }

            var values = _sqlHelper.GetFieldValues(fields);

            foreach (var el in values)
            {
                dataTable.Rows.Add(el.ToArray());
            }

            dataSet.Tables.Add(dataTable);

            GridData = dataSet.Tables[0].DefaultView;
        }

        #region Commands

        #region Show settings
        private DelegateCommand _showSettings;
        public ICommand ShowSettings
        {
            get
            {
                if (_showSettings == null)
                {
                    _showSettings = new DelegateCommand(this.OpenSettings);
                }
                return _showSettings;
            }
        }

        private void OpenSettings()
        {
            SettingsEdit settings = new SettingsEdit(_idJournal);
            settings.Show();
        }
        #endregion

        #region Add event
        private DelegateCommand _showAddEvent;
        public ICommand AddEvent
        {
            get
            {
                if (_showAddEvent == null)
                {
                    _showAddEvent = new DelegateCommand(this.ShowAddEvent);
                }
                return _showAddEvent;
            }
        }
        private void ShowAddEvent()
        {
            AddEvent addWin = new AddEvent(_idJournal);
            addWin.Show();
        }
        #endregion

        #region Update journal
        private DelegateCommand _updateJournal;
        public ICommand UpdateJournal
        {
            get
            {
                if (_updateJournal == null)
                {
                    _updateJournal = new DelegateCommand(this.UpdateDatagrid);
                }
                return _updateJournal;
            }
        }
        private void UpdateDatagrid()
        {
            this.LoadGrid(_idJournal);
        }
        #endregion



        #endregion
    }
}
